草庐IT

T14 Gen2

全部标签

C++14 我应该多久使用一次 constexpr?

我一直在读一本关于C++14/11的书。我刚读完有关constexpr关键字的一章。我知道它的用途,但我应该多久使用一次constexpr?我是否应该在我知道永远不会用于创建contstexpr对象的类的代码中使用它?(以防万一,因为它不需要我任何费用,对吧?) 最佳答案 C++14HowoftenshouldIuseconstexpr?在ScottMeyer's的第15项(尽可能使用constexpr)中有广泛的讨论。书EffectiveModernC++.这一项的概要是应该尽可能使用constexpr,因为constexpr函数

c++ - 本地类规则是否与 c++14 返回类型推导一致?

在阅读C++14的这一部分时(afreedraftN4141,closesttoC++14):9.8Localclassdeclarations[class.local][..]Thenameofalocalclassislocaltoitsenclosingscope.[..]Declarationsinalocalclassshallnotodr-use(3.2)avariablewithautomaticstoragedurationfromanenclosingscope.[Example://[..]voidf(){staticints;intx;//[..]structlo

c++ - 据我所知,根据 C++14 中的 §5.19/3 和 §5.19/2,这段代码不应该编译

但它在gcc4.9.0中编译。参见liveexample:#includestructA{constexprA():i(5){}int&&f(){returnstd::move(i);}inti;}a;A&&f(A&a){returnstd::move(a);}intmain(){Aa;intb[a.f()]{0,1,2,3,4};std::cout从§5.19/3我们有:Anintegralconstantexpressionisanexpressionofintegralorunscopedenumerationtype,implicitlyconvertedtoaprvalue,

c++ - 当函数返回类型为 bool 时,为什么不能在 C++14 中返回共享指针?

我正在使用g++并编写一个简单的函数:#includestd::shared_ptrptr;boolfails_compiling(){returnptr;}从我在界面中看到的内容来看,shared_ptr实现包括一个bool运算符,我什至可以像这样应用快速修复:returnstatic_cast(ptr);现在可以编译了。为什么返回算法不像if()和while()那样尝试自动转换为bool? 最佳答案 如果你结账std::shared_ptr的bool转换运算符,您会看到它被声明为:explicitoperatorbool()co

c++ - 这个 constexpr 虚函数技术是否违反了任何 C++11/C++14 规则?

前几天我在阅读C++文档时注意到,虽然字面量类型不能有虚成员,但这并不妨碍它们实现虚成员。或者至少我是这么理解的。这是我一直在玩的一段代码:#include//Someforwarddeclarations:enumclassliteral_id;structliteral_base;structliteral_a;structliteral_b;//Nowsomedefinitions:enumclassliteral_id{a,b};structliteral_base{virtualliteral_idmethod()constnoexcept=0;};structliteral

C++11/14/17,GCC 7 与 GCC 8 : Name lookup for friend class templates

我想弄清楚以下代码在GCC7中是否有效,但在GCC8.1中无效。代码的作用是:定义(并转发声明)类模板MyGoodFriend(在全局命名空间中)在inner命名空间中定义一个类模板Befriended使MyGoodFriend的所有特化成为Befriended的friend有问题的部分是templatefriendclassMyGoodFriend;我明白问题是什么了。GCC8.1要求我在friend声明中使用完全限定名称::MyGoodFriend-然而,GCC7对MyGoodFriend很满意。这是代码:templateclassMyGoodFriend;namespaceinn

c++ - C++11和C++14如何实现动态函数调用?

这是我希望能解释我想要实现的目标的代码。vectorints;vectordoubles;structArg{enumType{Int,Double};Typetype;intindex;};templatevoidCall(constF&f,constvector&args){//TODO://-Firstassertthatcountandtypesorargumentsofagreewith.//-Call"f(args)"}//Example:voidcopy(inta,double&b){b=a;}inttest(){Call(copy,{{Int,3},{Double,2}

c++ - Boost.Pointer 容器在 C++11/14 中被 std::unique_ptr 淘汰了吗?

是否std::unique_ptr使Boost.PointerContainer库在C++11/14中过时?在C++98/03中没有移动语义,也没有像shared_ptr这样的智能指针。与原始指针相比,具有引用计数相关的开销(对于引用计数block和互锁增量/减量)。所以像std::vector>如果与std::vector相比有开销.但是是std::vector>与std::vector一样高效(没有引用计数开销),和此外安全关于异常和自动销毁(即vector>析构函数将自动调用析构函数对于指针存储在T中的vector项)?如果是这样,Boost.PointerContainer在C

c++ - C++14 标准中哪里说不能在 constexpr 函数的定义中使用非 constexpr 函数?

例如,除非声明incr()constexpr,否则下面的代码不会编译:intincr(int&n){return++n;}constexprintfoo(){intn=0;incr(n);returnn;}查看C++14中的§7.1.5/3我们有:Thedefinitionofaconstexprfunctionshallsatisfythefollowingconstraints:(3.1)—itshallnotbevirtual(10.3);(3.2)—itsreturntypeshallbealiteraltype;(3.3)—eachofitsparametertypessha

c++ - 从 C++14 开始,尾随返回类型语法的合法使用

实际上是否有任何理由再使用以下语法:templateautoaccess(T&t,inti)->decltype(t[i]){returnt[i];}现在我们可以使用:templatedecltype(auto)access(T&t,inti){returnt[i];}尾随返回类型语法现在看起来有点多余? 最佳答案 推导的返回类型对SFINAE不友好。如果t[i],此重载将简单地退出重载集无效:templateautoaccess(T&t,inti)->decltype(t[i]){returnt[i];}而这种重载不会导致硬错误: